﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace iLangl.Model
{
	public class ContactRequestContext : BaseContext
	{
		public ContactRequestContext()
			: base()
		{

		}

			
		Contact contact = null;
		Contact[] contacts = null;
		public override void SetContext(object context)
		{
			base.SetContext(context);

			if (context is Contact)
			{
				contact = context as Contact;
			}

			if (context is Contact[])
			{
				contacts = context as Contact[];
			}
		}

		public void Write(XmlWriter writer)
		{
			if (contact != null)
			{
				writer.WriteStartElement("Contact");

				writer.WriteAttributeString("id", contact.Id);
				if (!String.IsNullOrEmpty(contact.Title))
					writer.WriteAttributeString("title", contact.Title);
				if (!String.IsNullOrEmpty(contact.FirstName))
					writer.WriteAttributeString("firstname", contact.FirstName);
				if (!String.IsNullOrEmpty(contact.LastName))
					writer.WriteAttributeString("lastname", contact.LastName);
				if (!String.IsNullOrEmpty(contact.Email))
					writer.WriteAttributeString("email", contact.Email);
				if (!String.IsNullOrEmpty(contact.Phone))
					writer.WriteAttributeString("phone", contact.Phone);
				if ((contact.Company != null) && (!string.IsNullOrEmpty(contact.Company.Id)))
					writer.WriteAttributeString("companyId", contact.Company.Id);
				InitExtendedAttributes(writer, contact);
				InitExtendedElements(writer, contact);
				writer.WriteEndElement();//root
			}
			else
				if (contacts != null)
				{
					writer.WriteStartElement("Contacts");

					ContactRequestContext contactrequestcontext = new ContactRequestContext();
					foreach (Contact icontact in contacts)
					{
						contactrequestcontext.SetContext(icontact);
						contactrequestcontext.Write(writer);
					}
					writer.WriteEndElement();
				}
		}
		public override object GetContext()
		{
			
				StringBuilder result;
				XmlWriter writer;

				CreateXmlWriter(out result, out writer);
				writer.WriteStartDocument();

				Write(writer);
				writer.WriteEndDocument();
				writer.Flush();
				return result.ToString();
	
		}
	}
}
